// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers','ngCordova'])

.run(function($ionicPlatform,$cordovaPush,$cordovaBarcodeScanner,$cordovaGeolocation) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)

    //初始化avos
    AV.initialize('32co2g21ef1quhvwjl9ok7u4upqvha6d8kv0vas2rstiqbly', 'n0xpioh693t6fa3l1kjyu32lbubrr0kj6tsupothw8xl21c9');
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
 var posOptions = {timeout: 10000, enableHighAccuracy: false};
  $cordovaGeolocation
      .getCurrentPosition(posOptions)
      .then(function (position) {
        var lat  = position.coords.latitude;
        var long = position.coords.longitude;
        alert('lat:'+lat);
        alert('long:'+long);
      }, function(err) {
        // error
      });

      $cordovaBarcodeScanner
              .scan()
              .then(function(barcodeData) {
                alert(JSON.stringify(barcodeData));
              }, function(error) {
                // An error occurred
              });
              });

      var iosConfig = {
        "badge": true,
        "sound": true,
        "alert": true
      };
      $cordovaPush.register(iosConfig).then(function(deviceToken) {
        // Success -- send deviceToken to server, and store for future use
        console.log("deviceToken: " + deviceToken)
      }, function(err) {
        alert("Registration error: " + err)
      });


      $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
        if (notification.alert) {
          navigator.notification.alert(notification.alert);
        }

        if (notification.sound) {
          var snd = new Media(event.sound);
          snd.play();
        }

        if (notification.badge) {
          $cordovaPush.setBadgeNumber(notification.badge).then(function(result) {
            // Success!
          }, function(err) {
            // An error occurred. Show a message to the user
          });
        }
      });

      // WARNING! dangerous to unregister (results in loss of tokenID)
      $cordovaPush.unregister(options).then(function(result) {
        // Success!
      }, function(err) {
        // Error
      });
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
    .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/menu.html',
    controller: 'AppCtrl'
  })

  .state('app.account', {
    url: '/account',
    views: {
      'menuContent': {
        templateUrl: 'templates/account.html',
        controller: 'AccountCtrl'
      }
    }
  })

   .state('app.baseinfo', {
        url: '/account/baseinfo',
        views: {
          'menuContent': {
            templateUrl: 'templates/account/baseinfo.html',
            controller: 'AccountCtrl'
          }
        }
      })

  .state('app.receiveOrder', {
    url: '/receiveOrder',
    views: {
      'menuContent': {
        templateUrl: 'templates/receiveOrder.html',
        controller:'receiveOrderCtrl'
      }
    }
  })

  .state('app.browse', {
      url: '/browse',
      views: {
        'menuContent': {
          templateUrl: 'templates/browse.html'
        }
      }
    })
    .state('app.orders', {
      url: '/orders',
      views: {
        'menuContent': {
          templateUrl: 'templates/orders.html',
          controller: 'PlaylistsCtrl'
        }
      }
    })

  .state('app.single', {
    url: '/playlists/:playlistId',
    views: {
      'menuContent': {
        templateUrl: 'templates/playlist.html',
        controller: 'PlaylistCtrl'
      }
    }
  });
  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/app/orders');
});
